package com.zhiyou100.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.entity.Lessee;
import com.zhiyou100.service.LesseeService;
import com.zhiyou100.util.object.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 租客信息 action
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-08-31 11:11
 */
@Controller
@RequestMapping("/lessee")
public class LesseeAction {
    @Autowired
    private LesseeService lesseeService;

    @RequestMapping("/list.action")
    public String listHouses(Model model,
                             @RequestParam(defaultValue = "1",  value = "pageNo") Integer pageNo
    ) {
        // 每页显示的记录数
        int pageSize = 10;

        // 分页查询
        PageHelper.startPage(pageNo, pageSize);

        // 获取所有的信息
        final List<Lessee> list = lesseeService.listLessees();

        // 使用 PageInfo<Lessee> 结果进行封装

        final PageInfo<Lessee> pageInfo = new PageInfo<>(list);

        model.addAttribute("pageInfoLessee", pageInfo);

        return "view/lessee/list";
    }

    @RequestMapping("/getOneById.action")
    public String getOneByHid(Integer lid, String method, Model model) {
        final Lessee lessee = lesseeService.selectOneByLid(lid);
        if (lessee != null) {
            // 有，放入区域中，跳转 到 详情页面
            if (method == null || method.trim().length() == 0) {
                // 跳转到 view/lessee/detail
                model.addAttribute("lessee", lessee);
                return "view/lessee/detail";
            } else {
                // 跳转到更新界面
                model.addAttribute("lessee", lessee);
                return "view/lessee/update";
            }

        } else {
            // 没有，返回列表
            return "view/lessee/list";
        }
    }

    @RequestMapping("/insertOne.action")
    public String insertOne(Lessee lessee, HttpServletRequest request) {

        String id = "lid";
        String addTime = "laddtime";
        if (ObjectUtil.onlyOneFiledIsNullAndTheFiledIsId(lessee, id, addTime)) {
            // lid 和  laddtime 字段为空，合法
            // 设置时间
            lessee.setLaddtime(new Date());
            lesseeService.insertOne(lessee);
        }

        return "/view/lessee/list";
    }

    @RequestMapping("/updateOne.action")
    public String updateOneByCid(Lessee lessee) {

        String addTime = "laddtime";
        if (ObjectUtil.onlyOneFiledIsNullAndTheFiledIsId(lessee, addTime)) {
            // laddtime 字段为空，合法,设置添加时间
            lessee.setLaddtime(new Date());
            lesseeService.updateOneLessee(lessee);
        }

        return "/view/lessee/list";
    }

    /**
     * 自定义一个 日期属性编辑器
     * yyyy-MM-dd'T'HH:mm:ss 针对 <input type="datetime-local" value="2020-08-28T20:49:16">
     *
     * @param request HttpServletRequest
     * @param binder  binder
     */
    @InitBinder
    public void myDateBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, true));
    }

    @RequestMapping("/deleteOneById.action")
    public String deleteOneByHid(Integer lid) {
        if (lid != null) {
            lesseeService.deleteOneByCid(lid);
        }
        return "view/lessee/list";
    }

}
